package com.collect.tanzania.zhiScene.zhiCollection.zhiMember.zhiPerformance.zhiB2;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.text.SpannableStringBuilder;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.swiperefreshlayout.widget.SwipeRefreshLayout;

import com.collect.tanzania.R;
import com.collect.tanzania.zhiAdater.zhiLevelDetail.ZHILevelDetailCollecterMemberB2;
import com.collect.tanzania.zhiBase.BaseFragment;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRCollectMemberPerformanceListEntity;
import com.collect.tanzania.zhiBean.zhiReceive.ZHIRCollecterPerformanceEntity;
import com.collect.tanzania.zhiBean.zhiSend.ZHISPerfermanceEntity;
import com.collect.tanzania.zhiGlobal.zhiAppSettings.ZHIProjectRouter;
import com.collect.tanzania.zhiHelper.ZHIMainProcessHelper;
import com.collect.tanzania.zhiScene.zhiCollection.zhiMember.zhiPerformance.zhiSort.ZHIMemberPerformanceCompare;
import com.collect.tanzania.zhiScene.zhiWidget.zhiDialog.ZHIDialogMessage;
import com.collect.tanzania.zhiUtils.ZHICalendarUtil;

import java.util.ArrayList;
import java.util.Collections;

import butterknife.BindView;

@SuppressLint("NonConstantResourceId")
public class ZHIPerformanceMemberB2Fragment extends BaseFragment<ZHIPerformanceMemberB2Presenter> implements ZHIPerformanceMemberB2Contract.View {

    @BindView(R.id.srl_swipe_layout)
    SwipeRefreshLayout srl_swipe_layout;

    @BindView(R.id.tv_page_title)
    TextView tv_page_title;

    @BindView(R.id.tv_cumulative_bonus)
    TextView tv_cumulative_bonus;

    @BindView(R.id.tv_cumulative_bonus_date)
    TextView tv_cumulative_bonus_date;
    @BindView(R.id.iv_cumulative_bonus_date)
    ImageView iv_cumulative_bonus_date;

    @BindView(R.id.rc_detail_list)
    RecyclerView rc_detail_list;

    @BindView(R.id.iv_sort)
    ImageView iv_sort;

    @BindView(R.id.iv_note)
    ImageView iv_note;
    @BindView(R.id.iv_filter)
    ImageView iv_filter;
    @BindView(R.id.tv_fcm_old_version)
    TextView tv_fcm_old_version;

    @BindView(R.id.ll_dis_date_bg)
    LinearLayout ll_dis_date_bg;

    @BindView(R.id.ll_repay_date_bg)
    LinearLayout ll_repay_date_bg;

    @BindView(R.id.ll_num_bg)
    LinearLayout ll_num_bg;

    @BindView(R.id.ll_settle_num_bg)
    LinearLayout ll_settle_num_bg;

    @BindView(R.id.ll_collected_rate_bg)
    LinearLayout ll_collected_rate_bg;

    @BindView(R.id.tv_fpmb2_yellow_card)
    TextView tv_fpmb2_yellow_card;
    @BindView(R.id.tv_fpmb2_green_card)
    TextView tv_fpmb2_green_card;

    @BindView(R.id.tv_fpmb2_total)
    TextView tv_fpmb2_total;
    @BindView(R.id.tv_fpmb2_ranking)
    TextView tv_fpmb2_ranking;

    private boolean mIsSortDisDateDes = true;
    private boolean mIsSortRepayDateDes = false;
    private boolean mIsSortNumDes = false;
    private boolean mIsSortSettleNumDes = false;
    private boolean mIsSortCollectedRateDes = false;

    private final ZHILevelDetailCollecterMemberB2 mZHILevelDetailCollecterMemberB2 = new ZHILevelDetailCollecterMemberB2();

    private ArrayList<ZHIRCollecterPerformanceEntity> mCustomerPerformanceEntityArrayList;

    /**
     * 是否为降序
     */
    private boolean mIsArraySortDesNow = true;

    @Override
    protected void initInjector() {
        mFragmentComponent.inject(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.fragment_perfermance_member_b2;
    }

    @SuppressLint("NotifyDataSetChanged")
    @Override
    protected void initView(View view) {
        try {
            ZHICalendarUtil.initDateTimeOf0706L();
            ZHICalendarUtil.initDateTimeOf0801L();

            srl_swipe_layout.setColorSchemeColors(getResources().getColor(R.color.theme_color_0));

            tv_page_title.setOnClickListener(v -> {
            });

            iv_note.setOnClickListener(v -> ZHIProjectRouter.toPerformanceCustomerBonusRules());
            tv_fcm_old_version.setOnClickListener(v -> ZHIProjectRouter.toPerformanceCollectMemberOld());

            iv_sort.setOnClickListener(v -> {
                try {
                    if (mCustomerPerformanceEntityArrayList == null) {
                        return;
                    }

                    if (mIsSortCollectedRateDes) {
                        mIsSortCollectedRateDes = false;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHIMemberPerformanceCompare.ZHIMemberPerformanceCompareBySettleRateASC());
                    } else {
                        mIsSortCollectedRateDes = true;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHIMemberPerformanceCompare.ZHIMemberPerformanceCompareBySettleRateDESC());
                    }

                    mZHILevelDetailCollecterMemberB2.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            ll_dis_date_bg.setOnClickListener(v -> {
                try {
                    if (mCustomerPerformanceEntityArrayList == null) {
                        return;
                    }

//                    if (mIsSortDisDateDes) {
//                        mIsSortDisDateDes = false;
//                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHIMemberPerformanceCompare.ZHIMemberPerformanceCompareByAssignDateASC());
//                    } else {
//                        mIsSortDisDateDes = true;
//                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHIMemberPerformanceCompare.ZHIMemberPerformanceCompareByAssignDateDESC());
//                    }
//
//                    mZHILevelDetailCollecterMemberB2.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            ll_repay_date_bg.setOnClickListener(v -> {
                try {
                    if (mCustomerPerformanceEntityArrayList == null) {
                        return;
                    }

//                    if (mIsSortRepayDateDes) {
//                        mIsSortRepayDateDes = false;
//                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHIMemberPerformanceCompare.ZHIMemberPerformanceCompareByRepayDateASC());
//                    } else {
//                        mIsSortRepayDateDes = true;
//                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHIMemberPerformanceCompare.ZHIMemberPerformanceCompareByRepayDateDESC());
//                    }
//
//                    mZHILevelDetailCollecterMemberB2.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            ll_num_bg.setOnClickListener(v -> {
                try {
                    if (mCustomerPerformanceEntityArrayList == null) {
                        return;
                    }

//                    if (mIsSortNumDes) {
//                        mIsSortNumDes = false;
//                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHIMemberPerformanceCompare.ZHIMemberPerformanceCompareByRepayAmountASC());
//                    } else {
//                        mIsSortNumDes = true;
//                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHIMemberPerformanceCompare.ZHIMemberPerformanceCompareByRepayAmountDESC());
//                    }
//
//                    mZHILevelDetailCollecterMemberB2.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            ll_settle_num_bg.setOnClickListener(v -> {
                try {
                    if (mCustomerPerformanceEntityArrayList == null) {
                        return;
                    }

                    if (mIsSortSettleNumDes) {
                        mIsSortSettleNumDes = false;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHIMemberPerformanceCompare.ZHIMemberPerformanceCompareByRepayAmountASC());
                    } else {
                        mIsSortSettleNumDes = true;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHIMemberPerformanceCompare.ZHIMemberPerformanceCompareByRepayAmountDESC());
                    }

                    mZHILevelDetailCollecterMemberB2.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            ll_collected_rate_bg.setOnClickListener(v -> {
                try {
                    if (mCustomerPerformanceEntityArrayList == null) {
                        return;
                    }

                    if (mIsSortCollectedRateDes) {
                        mIsSortCollectedRateDes = false;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHIMemberPerformanceCompare.ZHIMemberPerformanceCompareByBounsASC());
                    } else {
                        mIsSortCollectedRateDes = true;
                        Collections.sort(mCustomerPerformanceEntityArrayList, new ZHIMemberPerformanceCompare.ZHIMemberPerformanceCompareByBounsDESC());
                    }

                    mZHILevelDetailCollecterMemberB2.notifyDataSetChanged();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            });

            iv_filter.setOnClickListener(v -> {

            });

            iv_cumulative_bonus_date.setOnClickListener(v -> ZHIProjectRouter.toPerformanceCustomerHistory());
            tv_cumulative_bonus_date.setOnClickListener(v -> ZHIProjectRouter.toPerformanceCustomerHistory());

            initEvent();
            initData(true);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    @Override
    public void onResume() {
        super.onResume();
    }

    @Override
    public void onGetErrorInfo() {
        srl_swipe_layout.setRefreshing(false);
    }

    @Override
    public void onGetMemberB1B2PerfermanceList(ZHIRCollectMemberPerformanceListEntity zhirCollectMemberPerformanceListEntity, ZHISPerfermanceEntity zhisPerfermanceEntity) {
        try {
            srl_swipe_layout.setRefreshing(false);

            ///////////////////////
            tv_page_title.setText(zhirCollectMemberPerformanceListEntity.getLimitDateFormat());
            tv_fpmb2_yellow_card.setText(zhirCollectMemberPerformanceListEntity.getYellowCardFormat());
            tv_fpmb2_green_card.setText(zhirCollectMemberPerformanceListEntity.getFlowerFormat());
            ///////////////////////
            ArrayList<ZHIRCollecterPerformanceEntity> zhirCustomerPerformanceEntityArrayListOri = zhirCollectMemberPerformanceListEntity.getDataList();
            ArrayList<ZHIRCollecterPerformanceEntity> zhirCustomerPerformanceEntityArrayList = new ArrayList<>();

            for (int i = 0; i < zhirCustomerPerformanceEntityArrayListOri.size(); i++) {
                ZHIRCollecterPerformanceEntity zhirCustomerPerformanceEntity = zhirCustomerPerformanceEntityArrayListOri.get(i);
                zhirCustomerPerformanceEntity.setLimitDate(zhirCollectMemberPerformanceListEntity.getLimitDate());
                if (zhirCustomerPerformanceEntity.isAfter0706Date()) {
                    zhirCustomerPerformanceEntityArrayList.add(zhirCustomerPerformanceEntity);
                }
            }

            ///////////////////////
            tv_fpmb2_total.setText(zhirCollectMemberPerformanceListEntity.getTotalRankNums());
            tv_fpmb2_ranking.setText(zhirCollectMemberPerformanceListEntity.getRank());
            ///////////////////////
            tv_cumulative_bonus.setText(zhirCollectMemberPerformanceListEntity.getTotalBonusFormat());

            ///////////////////////
//            int totalNum = 0;
//            for (int i = 0; i < zhirCustomerPerformanceEntityArrayList.size(); i++) {
//                ZHIRCollecterPerformanceEntity zhirCustomerPerformanceEntity = zhirCustomerPerformanceEntityArrayList.get(i);
//                if (zhirCustomerPerformanceEntity.isHighLightFlag()) {
//                    totalNum += zhirCustomerPerformanceEntity.getAssignNumFormatInt();
//                }
//            }
//            tv_total_orders_num.setText(String.valueOf(totalNum));

            ///////////////////////
//            int settledNum = 0;
//            for (int i = 0; i < zhirCustomerPerformanceEntityArrayList.size(); i++) {
//                ZHIRCollecterPerformanceEntity zhirCustomerPerformanceEntity = zhirCustomerPerformanceEntityArrayList.get(i);
//                if (zhirCustomerPerformanceEntity.isHighLightFlag()) {
//                    settledNum += zhirCustomerPerformanceEntity.getRepayNumFormatInt();
//                }
//            }
//            tv_total_collected_num.setText(String.valueOf(settledNum));

            ///////////////////////
//            String collectRateStr = "0";
//            if (totalNum > 0) {
//                float totalNumL = totalNum * 1.0F;
//                float settledNumL = settledNum * 1.0F;
//                float collectRateL = settledNumL / totalNumL;
//                collectRateStr = StringUtil.numberFormatterThousandPercent(collectRateL + "");
//            }
//            tv_total_collected_rate.setText(collectRateStr);

            ///////////////////////
            Collections.sort(zhirCustomerPerformanceEntityArrayList, new ZHIMemberPerformanceCompare.ZHIMemberPerformanceCompareByAssignDateDESC());
            mCustomerPerformanceEntityArrayList = zhirCustomerPerformanceEntityArrayList;
            mZHILevelDetailCollecterMemberB2.setNewData(zhirCustomerPerformanceEntityArrayList);

        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @SuppressLint({"InflateParams", "SetTextI18n"})
    public void initEvent() {
        srl_swipe_layout.setOnRefreshListener(() -> initData(false));

        mZHILevelDetailCollecterMemberB2.setEmptyView(getRefreshEmptyView());

        rc_detail_list.setLayoutManager(new GridLayoutManager(this.getActivity(), 1));
        rc_detail_list.setAdapter(mZHILevelDetailCollecterMemberB2);
    }

    public void initData(boolean showDialog) {
        ZHISPerfermanceEntity zhisPerfermanceEntity = getCaseSearchParams();
        assert mPresenter != null;
        mPresenter.getMemberB1B2PerfermanceList(getActivity(), zhisPerfermanceEntity, showDialog);
    }

    public void refreshData() {
        initData(true);
    }

    private ZHISPerfermanceEntity getCaseSearchParams() {
        //return mZHIRootGroupFragmentInterface.onGetPerfermanceFilterParams();

//        String gStartDate = ZHICalendarUtil.getCurMonthFirstDayFormatString();
//        String gEndDate = ZHICalendarUtil.getCurMonthCurDayFormatString();

        ZHISPerfermanceEntity zhisPerfermanceEntity = new ZHISPerfermanceEntity();
        zhisPerfermanceEntity.setLeven(ZHIMainProcessHelper.getInstance().getLoginEntity().getDefaultLevenApiFormat());
        return zhisPerfermanceEntity;
    }

    private View getRefreshEmptyView() {
        View emptyView = getLayoutInflater().inflate(R.layout.view_empty_no_data, null);
        emptyView.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
        emptyView.setOnClickListener(v -> initData(true));
        return emptyView;
    }

    private void showBonusDialog() {
        ZHIDialogMessage.showDialog(getActivity(), null, new SpannableStringBuilder(""), true, null);
    }
}
